package cn.itcast.up32.up_web.controller;

import cn.itcast.up.common.HDFSUtils;
import cn.itcast.up.common.HdfsTools;
import cn.itcast.up32.up_web.bean.HttpResult;
import cn.itcast.up32.up_web.bean.dto.ModelDto;
import cn.itcast.up32.up_web.bean.dto.TagDto;
import cn.itcast.up32.up_web.bean.dto.TagModelDto;
import cn.itcast.up32.up_web.service.TagService;
import cn.itcast.up32.up_web.util.HttpStatus;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
//@RequestMapping("tags")
public class TagController {

    @Autowired
    private TagService tagService;

    //接收123级标签的创建请求
    @PutMapping("/tags/relation")
    @ResponseBody //将响应转换为Json
    public void pugTags(@RequestBody List<TagDto> tags) {
        tagService.save(tags);
    }

    /**
     * 按照Pid进行标签查询
     *
     * @param pid
     */
    @GetMapping("tags")
    public HttpResult<List<TagDto>> getTags(Long pid, Integer level) {
        List<TagDto> list = null;
        if (pid == null) {
            //如果Pid为null,按照等级进行查询
            list = tagService.findByLevel(level);
        }
        if (level == null) {
            //如果等级为null,按照pid进行查询
            list = tagService.findByPid(pid);
        }
        //将数据返回.
        return new HttpResult<List<TagDto>>(HttpStatus.SUCCESS, "查询成功", list);
    }

    @PutMapping("tags/model")
    public HttpResult putTagModel(@RequestBody TagModelDto tagModel) {
        System.out.println(tagModel);
        //调用Service开始保存标签和模型
        tagService.addTagModel(tagModel.getTag(), tagModel.getModel());
        return new HttpResult(HttpStatus.SUCCESS, "成功", null);
    }

    /**
     * 此方法获取4级标签列表.
     *
     * @param pid 只能是3级的ID
     * @return
     */
    @GetMapping("tags/model")
    public HttpResult<List<TagModelDto>> getFourTag(Long pid) {
        //根据4级标签的Pid进行查询,获取对应的标签和模型
        List<TagModelDto> list = tagService.getFourTag(pid);
        return new HttpResult(HttpStatus.SUCCESS, "成功", list);
    }

    /**
     * 添加5级标签规则
     *
     * @param tagDto
     * @return
     */
    @PutMapping("tags/data")
    public HttpResult<List<TagModelDto>> addFiveTag(@RequestBody TagDto tagDto) {
        tagService.addFiveTag(tagDto);
        return new HttpResult(HttpStatus.SUCCESS, "成功", null);
    }


    /**
     * 文件上传
     *
     * @param file 字段名字必须和前端传过来的名字保持一致.
     * @return
     */
    @PostMapping("tags/upload")
    public HttpResult uploadFile(MultipartFile file) {
        //本地缓存的Jar路径
        String localFilePath = "C:\\My_Data\\IDEA_WorkSpace\\userprofile32\\model.jar";
        //构建HDFS路径 , 使用uuid进行组织.
        String path = "/tmp/jars/" + UUID.randomUUID().toString() + ".jar";
        String contentType = file.getContentType();
        System.out.println(contentType);
        long size = file.getSize();
        System.out.println(size);
        try {
            //接收文件,接收到本地
            InputStream inputStream = file.getInputStream();
            //将文件,先保存到本地,这只是一个临时缓存
            IOUtils.copy(inputStream, new FileOutputStream(new File(localFilePath)));
            //将文件进行上传.
            HDFSUtils.getInstance().copyFromFile(localFilePath, path);
        } catch (IOException e) {
            e.printStackTrace();
            return new HttpResult(HttpStatus.ERROR_UPLOAD, "上传失败", null);
        }
        return new HttpResult(HttpStatus.SUCCESS, "上传成功", "hdfs://bd001:8020" + path);
    }

    /**
     * 启动模型任务
     * @param id 需要启动的标签ID 462
     * @param modelDto 只有一个state状态信息
     * @return
     */
    @PostMapping("tags/{id}/model")
    public HttpResult changeModelState(@PathVariable Long id, @RequestBody ModelDto modelDto){
        tagService.updateModelState(id, modelDto.getState());
        return new HttpResult(HttpStatus.SUCCESS, "执行成功", null);
    }
}
